package org.scboy.common.dynamic.node.logicNode;

import java.util.List;

import org.scboy.common.annotation.ScriptNode;
import org.scboy.common.dynamic.node.BaseNode;
import org.scboy.utils.entitys.coobi.Model;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;

@ScriptNode(value="LOGIC_FOREACH")
public class LogicForeachNode extends BaseNode {

	@Override
	public void create(JSONObject object) {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object run() {
		/**
		 * 测试日志
		 */
		if(this.script.isTest()) {
			this.script.appendMsg("----->开始执行："+getNodeName()+" 节点ID："+this.getId());
		}
		
		//输出流
		BaseNode runTo = getRunTo("to");
		
		boolean runOver=Convert.toBool(getData("_runOver"));
		
		if(runOver) {
			
			/**
			 * 测试日志
			 */
			if(this.script.isTest()) {
				this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
				this.script.appendMsg("执行下个节点："+runTo.getNodeName()+" 节点ID："+runTo.getId());
			}
			
			return runTo.run();
		}else {
			Long index=Convert.toLong(getData("_index"));
			
			if(index==null) {
				index=0L;
				setData("_index", index);
			}
			
			//循环流
			BaseNode foreachTo = getRunTo("foreach");
			Object params=getSourceData("params", true, Object.class);
			
			//设置循环流分支根为自己
			foreachTo.setBranchNode(this);
			
			//判断数据类型
			if(params instanceof List) {
				List<Model> datas=(List<Model>)params;
				if(index < datas.size()) {
					this.setData("entity", datas.get(Convert.toInt(index)));
					this.setData("index", Convert.toInt(index));
					return foreachTo.run();
				}else {
					setData("_runOver", true);
					
					/**
					 * 测试日志
					 */
					if(this.script.isTest()) {
						this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
						this.script.appendMsg("执行下个节点："+runTo.getNodeName()+" 节点ID："+runTo.getId());
					}
					
					return runTo.run();
				}
			}else if(params instanceof Integer) {
				int count=Convert.toInt(params);
				if(index < count) {
					this.setData("index", Convert.toInt(index));
					
					/**
					 * 测试日志
					 */
					if(this.script.isTest()) {
						this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
						this.script.appendMsg("执行下个节点："+foreachTo.getNodeName()+" 节点ID："+foreachTo.getId());
					}
					
					return foreachTo.run();
				}else {
					setData("_runOver", true);
					
					/**
					 * 测试日志
					 */
					if(this.script.isTest()) {
						this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
						this.script.appendMsg("执行下个节点："+runTo.getNodeName()+" 节点ID："+runTo.getId());
					}
					
					
					return runTo.run();
				}
			}else if(params instanceof Long) {
				long count=Convert.toLong(params);
				if(index < count) {
					this.setData("index", Convert.toInt(index));
					
					/**
					 * 测试日志
					 */
					if(this.script.isTest()) {
						this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
						this.script.appendMsg("执行下个节点："+foreachTo.getNodeName()+" 节点ID："+foreachTo.getId());
					}
					
					return foreachTo.run();
				}else {
					setData("_runOver", true);
					
					/**
					 * 测试日志
					 */
					if(this.script.isTest()) {
						this.script.appendMsg("缓存数据：\r\n"+entityToString(this.getDataAll()));
						this.script.appendMsg("执行下个节点："+runTo.getNodeName()+" 节点ID："+runTo.getId());
					}
					
					return runTo.run();
				}
			}else {
				throw new RuntimeException(getNodeName()+"：无法识别的数据类型。");
			}
		}
	}
}
